﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace GPM
{
    public partial class FrmModule : BaseForm
    {
        public GridCheckMarksSelection selection;
        public FrmModule()
        {
            InitializeComponent();
            this.BarComponents = components;
        }

        private void FrmModule_Load(object sender, EventArgs e)
        {
            selection = new GridCheckMarksSelection(this.gvModule);
            RefreshModule();
        }

        public void RefreshModule()
        {
            ModuleController dc = new ModuleController();
            this.gcModule.DataSource = dc.GetAllModules();
        }

        private void barQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshModule();
        }

        private void barAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmEditModule frmedit = new FrmEditModule();
            frmedit.ShowDialog(this);
        }

        private void barEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selection.SelectedCount != 1)
            {
                ExMessageBox.Show("提示信息：请选中一条记录编辑", AppSetter.SoftName);
                return;
            }
            Module info = selection.Selection[0] as Module;
            if (null == info)
            {
                ExMessageBox.Show("提示信息：选中的记录不存在", AppSetter.SoftName);
                return;
            }
            FrmEditModule frmedit = new FrmEditModule("1", info.module_id);
            frmedit.ShowDialog(this);
        }

        private void barDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (0 == selection.SelectedCount)
            {
                ExMessageBox.Show("提示信息：请选中要删除的记录！", AppSetter.SoftName);
                return;
            }

            if (ExMessageBox.Confirm("提示信息：确定要删除选中的记录吗？", AppSetter.SoftName) == DialogResult.No)
                return;

            IList<int> deptids = new List<int>();
            foreach (object o in selection.Selection)
            {
                Module dept = o as Module;
                if (null != dept)
                    deptids.Add(dept.module_id);
            }
            try
            {
                ModuleController dc = new ModuleController();
                dc.DeleteModules(deptids);
                ExMessageBox.Show("提示信息：删除成功！", AppSetter.SoftName);
                RefreshModule();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

    }
}
